public class MySingleList {
    static class ListNodes{
        public int val;
        public ListNodes next;
        public ListNodes(int val){
            this.val = val;
        }
    }
    public ListNodes head;
    public void creatList(){
        ListNodes node1 = new ListNodes(12);
        ListNodes node2 = new ListNodes(23);
        ListNodes node3 = new ListNodes(34);
        ListNodes node4 = new ListNodes(45);
        ListNodes node5 = new ListNodes(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }

    public void display(){
        ListNodes cur = head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void addFirst(int data){
        ListNodes nodes = new ListNodes(data);
        nodes.next = head;
        head = nodes;
    }
    public void addLast(int data){
        ListNodes nodes = new ListNodes(data);
        ListNodes cur = head;
        if (cur == null){
            head = nodes;
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = nodes;
    }
    public int  size(){
        ListNodes cur = head;
        int count = 0;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public ListNodes findnodeSubone(int index){
        ListNodes cur = head;
        while (index -1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    public void addIndex(int index,int data){
        if (index < 0 || index > size()){
            System.out.println("位置不合法！");
        }
        if (index == 0 ){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        ListNodes cur = findnodeSubone(index);
        ListNodes nodes = new ListNodes(data);
        nodes.next = cur.next;
        cur.next = nodes;
    }

    public boolean contains(int key){
        ListNodes cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public ListNodes Search(int key){
        ListNodes cur = head;
        while (cur.next != null){
            if (cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    public void remove(int key){
        if (head == null){
            return;
        }
        if (head.val == key){
            head = head.next;
            return;
        }
        ListNodes cur = Search(key);
        if (cur ==null){
            System.out.println("没有要删除的数字");
            return;
        }
        ListNodes del = cur.next;
        cur.next = del.next;
    }


    public void removeAll(int key){
        ListNodes cur = head;
        ListNodes prev = Search(key);
        if (head == null){
            return;
        }
        while (cur != null){
            if (cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }
            else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key){
            head = head.next;
        }
    }

    public void clean(){
        this.head = null;
    }
}
